let post_id = sessionStorage.getItem("post_id");
let token = sessionStorage.getItem("token");
//确定模态框
$("#my-alert").on('close.modal.amui', function () {
    window.location.reload();
});
//模态框
$("#my-popup").on('close.modal.amui', function () {
    $("#title").val("");
    $("#out_link").val("");
    editor.html("");
    $("#time3").attr("value", "");
});
//封装提交后的提示
function prompt(data, title, modal) {
    if (data.success === true) {
        $(".users").html("" + title + "");
        $(".alert").html(data.message);
        $('#my-alert').modal({ target: '#my-alert' });
        $("#" + modal + "").modal('close');

    } else {
        $(".users").html("" + title + "");
        $(".alert").html(data.message);
        $('#my-alert').modal({ target: '#my-alert' });
        $("#" + modal + "").modal('close');
    }
}
// kindeditor
KindEditor.ready(function (K) {
    editor = K.create('#editor_id', {
        uploadJson: testurl + 'kindeditor/'

    });
});
$(function () {



    //分页
    let pageNum = 1;
    $(".first").click(function () {
        pageNum = 1;
        loadData(pageNum);
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });
    $(".last").click(function () {
        pageNum = pages;
        loadData(pageNum);
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });
    $(".up").click(function () {
        if (pageNum === 1) {
            return false;
        } else {
            pageNum--;
            loadData(pageNum);
        }
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });
    $(".down").click(function () {
        if (pageNum === pages) {
            return false;
        } else {
            pageNum++;
            loadData(pageNum);
        }
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });


    loadData(1);
    let user_ids = [];
    let checkedLength = "";
    $("#dbdelete").click(function () {
        $.each($('#show input:checkbox:checked'), function () {
            let userId = $(this).val();

            user_ids.push(userId);
        });
        checkedLength = $('#show input:checkbox:checked').length;
        if (checkedLength <= 0) {
            alert("请选中至少一条任免！");
            return false;
        } else {
            $.ajax({
                url: testurl + "news/deletes",
                type: "post",
                headers: { "Authorization": "Bearer " + token },
                contentType: "application/json",
                data: JSON.stringify({
                    "ids": user_ids //id数组
                }),
                success: function (data) {
                    prompt(data, "删除任免", "")
                }
            })
        }
    })
});
//页数
let pages = 1;
function loadData(pageNum) {
    $.ajax({

        url: testurl + "news/list",
        type: "post",
        headers: { "Authorization": "Bearer " + token },
        contentType: "application/json",
        data: JSON.stringify({
            "pageNum": pageNum,
            "type": "2"
        }),
        success: function (data) {
            let dbmessage = data.obj;
            ssd = Math.ceil(data.pageTotal / data.pageSize);
            pages = ssd;
            str = "";
            $.each(dbmessage, function (index, values) {
                if (values.deputy_group_name === undefined) {
                    values.deputy_group_name = "";
                }
                if (values.deputy_team_name === undefined) {
                    values.deputy_team_name = "";
                }
                if (values.post_name === undefined) {
                    values.post_name = "";
                }
                if (values.deputy_no === undefined) {
                    values.deputy_no = "";
                }
                str += " <tr class=\"gradeX\">\n" +
                    "                            <td><input name='selectAll'  class='checked' value=" + values.id + " type='checkbox'></td>\n" +
                    "                                        <td><a style=\"cursor:pointer\" data-am-modal=\"{target:'#modal1'}\" onclick=\"getMsg(" + values.id + ")\">" + values.title + "</a></td>\n" +
                    "                                        <td><a download href=\"" + testurl + "" + values.file_path + "\">" + values.file_name + "</a></td>\n" +
                    "                                        <td>" + values.create_user_name + "</td>\n" +
                    "                                        <td>" + values.create_times + "</td>\n" +
                        "      <td>\n" +
                    "    <button class=\"btnImg60 quan_xian\" rules=\"ren_xiu\"  data-am-modal=\"{target: '#my-popup'}\"  onclick='editor1(" + values.id + ");'>修改</button>\n" +
                    "    <button class=\"btnImg60\ data-am-modal=\"{target: '#modal1'}\"  onclick='getMsg(" + values.id + ");'>查看</button>\n" +
                            "     </td>\n"+
                    "                                    </tr>";

            });
            $("#show").html(str);
            rules();
        }
    });
}


//选中所有的checkbox框
$('#checkAll[name="selectAll"]').click(function () {
    if ($(this).is(':checked')) {
        $('#show input[name="selectAll"]').each(function () {

            $(this).prop("checked", true);
        });
    } else {
        $('input[name="selectAll"]').each(function () {
            $(this).removeAttr("checked", false);
        });
    }
});




// 添加任免
function submitMsg() {
    // 图片上传
    let file_path = "";
    let file_name = "";
    let formData = new FormData();
    let pic1 = document.getElementById("fileUpload1").files;

    for (let j = 0; j < pic1.length; j++) {
        formData.append("" + j + "", pic1[j]);
    }
    $.ajax({
        url: testurl + "news/file",
        type: 'post',
        headers: { "Authorization": "Bearer " + token },
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
            console.log(data)
            if (pic1[0] != undefined && pic1[0].name != '') {
                $("#upfile1").attr("name", data.obj.name);
                $("#upfile1").attr("path", data.obj.path);
            }
            file_path = data.obj.path;
            file_name = data.obj.name;

        },
        error: function (data) {
            console.log(data);
        }
    });
    let pic_path = "";
    let pic_name = "";
    let formData2 = new FormData();
    let pic2 = document.getElementById("fileUpload2").files;

    for (let j = 0; j < pic2.length; j++) {
        formData2.append("" + j + "", pic2[j]);
    }
    $.ajax({
        url: testurl + "news/file",
        type: 'post',
        headers: { "Authorization": "Bearer " + token },
        data: formData2,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
            console.log(data)
            if (pic2[0] != undefined && pic2[0].name != '') {
                if (pic1[0] != undefined) {
                    $("#upfile2").attr("name", data.obj.name);
                    $("#upfile2").attr("path", data.obj.path);
                } else {
                    $("#upfile2").attr("name", data.obj.name);
                    $("#upfile2").attr("path", data.obj.path);
                }
            }
            pic_path = data.obj.path;
            pic_name = data.obj.name;
        },
        error: function (data) {
            console.log(data);
        }
    });
    let title = $("#title").val();
    let out_link = $("#out_link").val();
    let pub_time = $("#time3").val();
    let content = editor.html();

    if (title === "" || pub_time === "") {
        alert("请填写任免标题和发布时间！");
        return false;
    } else {


        $.ajax({
            async: false,
            url: testurl + "news/",
            type: "post",
            headers: { "Authorization": "Bearer " + token },
            contentType: "application/json",
            data: JSON.stringify({
                "type": "2",
                "title": title,//标题
                "file_path": file_path,
                "file_name": file_name,
                "pic_path": pic_path,
                "pic_name": pic_name,
                "out_link": out_link,
                "content": content,
                "pub_time": pub_time
            }),
            success: function (data) {
                prompt(data, "添加任免", "my-popup")
            }
        })
    }


}
//修改
function editor1(e) {

    $("#getId").val(e);
    $("#add").hide();
    $("#rechange").show();
    $("#file1").show();
    $("#file2").show();
    $.ajax({
        url: testurl + "news/detail",
        type: "post",
        headers: { "Authorization": "Bearer " + token },
        contentType: "application/json",
        data: JSON.stringify({
            "id": e
        }),
        success: function (data) {
            console.log(data);
            let aaa = data.obj;
            if (aaa.file_name === undefined || aaa.file_path === undefined) {
                $("#file1").html();
            } else {
                $("#file1").html("<a style=\"color: blue\" target=\"_blank\" href=\"" + testurl + "" + aaa.file_path + "\">" + aaa.file_name + "</a><input style=\"height: 13px\" type=\"checkbox\" name=\"check1\"  >删除");
                $("#upfile1").attr("name", aaa.file_name);
                $("#upfile1").attr("path", aaa.file_path);
            }
            if (aaa.pic_name === undefined || aaa.pic_path === undefined) {
                $("#file2").html();
            } else {
                $("#file2").html("<a style=\"color: blue\" target=\"_blank\" href=\"" + testurl + "" + aaa.pic_path + "\">" + aaa.pic_name + "</a><input style=\"height: 13px\" type=\"checkbox\" name=\"check2\"   >删除");
                $("#upfile2").attr("name", aaa.pic_name);
                $("#upfile2").attr("path", aaa.pic_path);
            }
            $("#title").val(aaa.title);
            $("#out_link").val(aaa.out_link);
            editor.html(aaa.content);
            $("#time3").attr('value', aaa.pub_times);
        }
    })
}
//清楚文件
function clearFile() {
    $("#file1").hide();
    $("#file2").hide();
    $("#add").show();
    $("#rechange").hide();
}
//修改
function rechangeMsg() {

    let id = $("#getId").val();
    let title = $("#title").val();
    let out_link = $("#out_link").val();
    let pub_time = $("#time3").val();
    let content = editor.html();


    // 图片上传
    let file_path = "";
    let file_name = "";
    let formData = new FormData();
    let pic1 = document.getElementById("fileUpload1").files;

    for (let j = 0; j < pic1.length; j++) {
        formData.append("" + j + "", pic1[j]);
    }
    $.ajax({
        url: testurl + "news/file",
        type: 'post',
        headers: { "Authorization": "Bearer " + token },
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
            console.log(data)
            if (pic1[0] != undefined && pic1[0].name != '') {
                $("#upfile1").attr("name", data.obj.name);
                $("#upfile1").attr("path", data.obj.path);
            }
            file_path = data.obj.path;
            file_name = data.obj.name;

        },
        error: function (data) {
            console.log(data);
        }
    });
    let pic_path = "";
    let pic_name = "";
    let formData2 = new FormData();
    let pic2 = document.getElementById("fileUpload2").files;

    for (let j = 0; j < pic2.length; j++) {
        formData2.append("" + j + "", pic2[j]);
    }
    $.ajax({
        url: testurl + "news/file",
        type: 'post',
        headers: { "Authorization": "Bearer " + token },
        data: formData2,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
            console.log(data)
            if (pic2[0] != undefined && pic2[0].name != '') {
                if (pic1[0] != undefined) {
                    $("#upfile2").attr("name", data.obj.name);
                    $("#upfile2").attr("path", data.obj.path);
                } else {
                    $("#upfile2").attr("name", data.obj.name);
                    $("#upfile2").attr("path", data.obj.path);
                }
            }
            pic_path = data.obj.path;
            pic_name = data.obj.name;
        },
        error: function (data) {
            console.log(data);
        }
    });

    if ($("#upfile1").attr("path") != '') {
        if ((pic1[0] == undefined || pic1[0].name == '') && ($("input[name='check1']").length == 0 || $("input[name='check1']")[0].checked)) {
        } else {
            file_name = $("#upfile1").attr("name");
            file_path = $("#upfile1").attr("path");

        }
    }

    if ($("#upfile2").attr("path") != '') {
        if ((pic2[0] == undefined || pic2[0].name == '') && ($("input[name='check2']").length == 0 || $("input[name='check2']")[0].checked)) {

        } else {
            pic_name = $("#upfile2").attr("name");
            pic_path = $("#upfile2").attr("path");

        }
    }






    $.ajax({
        url: testurl + "news/update",
        type: "post",
        headers: { "Authorization": "Bearer " + token },
        contentType: "application/json",
        data: JSON.stringify({
            "id": id,
            "type": "2", //1任免 2公告
            "title": title,//标题
            "file_path": file_path,
            "file_name": file_name,
            "pic_path": pic_path,
            "pic_name": pic_name,
            "out_link": out_link,
            "pub_time": pub_time,
            "content": content
        }),
        success: function (data) {
            prompt(data, "修改任免", "my-popup")
        }
    })
}
//标题点开
function getMsg(e) {
    $.ajax({
        url: testurl + "news/detail",
        type: "post",
        headers: { "Authorization": "Bearer " + token },
        contentType: "application/json",
        data: JSON.stringify({
            "id": e
        }),
        success: function (data) {
            console.log(data);
            $("#title1").html(data.obj.title);
            $("#create_times").html(data.obj.create_times);
            $("#create_user_name").html(data.obj.create_user_name);
            $("#title2").html(data.obj.title);
            $("#content1").html(data.obj.content);

        }
    })
}